<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :maskClosable="false"
    :ok-button-props="{ props: { disabled: disableSubmit } }"
    :fullscreen="true"
    @close="close"
    cancelText="关闭"
  >
    <div style="margin: 0 auto" :class="{ isCheck: isCheck }">
      <a-form-model ref="form" :rules="validatorRules" :model="queryParam">
        <table class="table table-striped table-bordered" align="center" valign="center">
          <tr>
            <td class="column title" colspan="12">基本信息</td>
          </tr>
          <tr>
            <td class="column">名称</td>
            <td class="value">
              <a-form-model-item label="" prop="name">
                <a-input :disabled="disableSubmit" v-model="queryParam.name"></a-input>
              </a-form-model-item>
            </td>
            <td class="column">性别</td>
            <td class="value">
              <a-form-model-item label="" prop="sex">
                <j-dict-select-tag
                  style="width: 100%"
                  v-model="queryParam.sex"
                  dict-code="sex1"
                  :disabled="disableSubmit"
                />
              </a-form-model-item>
            </td>
            <td class="column">出生日期</td>
            <td class="value">
              <a-form-model-item label="" prop="birthday">
                <j-date style="width: 100%" v-model="queryParam.birthday" :disabled="disableSubmit"></j-date>
              </a-form-model-item>
            </td>
            <td class="column">民族</td>
            <td class="value" colspan="3">
              <a-input :disabled="disableSubmit" v-model="queryParam.nationality"></a-input>
            </td>
          </tr>
          <tr>
            <td class="column">籍贯</td>
            <td class="value">
              <a-input :disabled="disableSubmit" v-model="queryParam.birthplace"></a-input>
            </td>
            <td class="column">政治面貌</td>
            <td class="value">
              <a-form-model-item label="" prop="politicalStatus">
                <j-dict-select-tag
                  style="width: 100%"
                  v-model="queryParam.politicalStatus"
                  dict-code="political_status"
                  :disabled="disableSubmit"
                />
              </a-form-model-item>
            </td>
            <td class="column">工作时间</td>
            <td class="value">
              <a-form-model-item label="" prop="startWorkTime">
                <j-date style="width: 100%" :disabled="disableSubmit" v-model="queryParam.startWorkTime"></j-date>
              </a-form-model-item>
            </td>
            <td class="column">学历</td>
            <td class="value">
              <a-form-model-item label="" prop="education">
                <a-input :disabled="disableSubmit" v-model="queryParam.education"></a-input>
              </a-form-model-item>
            </td>
          </tr>
          <tr>
            <td class="column">学位</td>
            <td class="value">
              <a-form-model-item label="" prop="science">
                <a-input :disabled="disableSubmit" v-model="queryParam.science"></a-input>
              </a-form-model-item>
            </td>
            <td class="column">毕业院校</td>
            <td class="value">
              <a-input :disabled="disableSubmit" v-model="queryParam.school"></a-input>
            </td>
            <td class="column">所学专业</td>
            <td class="value">
              <a-input :disabled="disableSubmit" v-model="queryParam.profession"></a-input>
            </td>
            <td class="column">电话</td>
            <td class="value">
              <a-input :disabled="disableSubmit" v-model="queryParam.phone"></a-input>
            </td>
          </tr>
          <tr>
            <td class="column">邮箱</td>
            <td class="value">
              <a-input :disabled="disableSubmit" v-model="queryParam.email"></a-input>
            </td>
            <td class="column">登录名</td>
            <td class="value">
              <a-form-model-item label="" prop="loginName">
                <a-input
                  :disabled="disableSubmit || (model.id ? true : false)"
                  v-model="queryParam.loginName"
                ></a-input>
              </a-form-model-item>
            </td>
            <td class="column">密码</td>
            <td class="value">
              <a-form-model-item label="" prop="password">
                <a-input v-if="!model.id" v-model="queryParam.password"></a-input>
                <a-input-password v-else :disabled="true" v-model="queryParam.password" />
              </a-form-model-item>
            </td>
            <!--            <td class="column">权限</td>-->
            <!--            <td class="value">-->
            <!--              <a-form-model-item label="" prop="cate">-->
            <!--                <a-tree-select-->
            <!--                  treeDefaultExpandAll-->
            <!--                  v-model="queryParam.cate"-->
            <!--                  style="width: 100%"-->
            <!--                  :tree-data="treeData"-->
            <!--                  tree-checkable-->
            <!--                  :disabled="disableSubmit"-->
            <!--                />-->
            <!--              </a-form-model-item>-->
            <!--            </td> -->
          </tr>
          <tr>
            <td class="column title" colspan="12">单位信息</td>
          </tr>
          <tr>
            <td class="column">工作单位</td>
            <td class="value">
              <a-form-model-item label="" prop="unitName">
                <a-input
                  :disabled="disableSubmit"
                  v-model="queryParam.unitName"
                  @click.native="openOrganModel"
                ></a-input>
              </a-form-model-item>
            </td>
            <td class="column">职务</td>
            <td class="value">
              <a-input :disabled="disableSubmit" v-model="queryParam.position"></a-input>
            </td>
            <td class="column">地址</td>
            <td class="value">
              <a-input :disabled="disableSubmit" v-model="queryParam.address"></a-input>
            </td>
            <td class="column">邮编</td>
            <td class="value">
              <a-input :disabled="disableSubmit" v-model="queryParam.postCode"></a-input>
            </td>
          </tr>
          <tr>
            <td class="column title" colspan="12">技术职称</td>
          </tr>
          <tr>
            <td class="column">技术职称</td>
            <td class="value">
              <a-input :disabled="disableSubmit" v-model="queryParam.titleStructure"></a-input>
            </td>
            <td class="column">发证单位</td>
            <td class="value">
              <a-input :disabled="disableSubmit" v-model="queryParam.issuingUnit"></a-input>
            </td>
            <td class="column">证书编号</td>
            <td class="value">
              <a-input :disabled="disableSubmit" v-model="queryParam.certno"></a-input>
            </td>
            <td class="column">发证时间</td>
            <td class="value">
              <j-date style="width: 100%" :disabled="disableSubmit" v-model="queryParam.issuingTime"></j-date>
            </td>
          </tr>
          <tr>
            <td class="column title" colspan="12">职业证书</td>
          </tr>
        </table>
        <div>
          <a-row type="flex" style="margin-bottom: 10px; height: 50px; line-height: 50px" :gutter="0">
            <a-col style="text-align: center; border: 1px solid #b8e6ff; background: #f3faff" :span="5">证书名称</a-col>
            <a-col style="text-align: center; border: 1px solid #b8e6ff; background: #f3faff" :span="5">注册单位</a-col>
            <a-col style="text-align: center; border: 1px solid #b8e6ff; background: #f3faff" :span="5">证书编号</a-col>
            <a-col style="text-align: center; border: 1px solid #b8e6ff; background: #f3faff" :span="5">发证时间</a-col>
            <a-col style="text-align: center; border: 1px solid #b8e6ff; background: #f3faff" :span="4">
              <span v-if="disableSubmit">操作</span>
              <span v-else>
                <a-button @click="addRowCustom" style="padding: 0 5px" size="small" type="primary" icon="plus"
                  >新增</a-button
                >
              </span>
            </a-col>
          </a-row>

          <a-row type="flex" style="margin-bottom: 10px" :gutter="16" v-for="(item, index) in dataSource" :key="index">
            <a-col :span="6" style="display: none">
              <a-form-model-item>
                <a-input placeholder="id" v-model="item.id" />
              </a-form-model-item>
            </a-col>
            <a-col :span="5">
              <a-form-model-item>
                <a-input
                  style="padding-left: 5px"
                  :disabled="disableSubmit"
                  placeholder="请输入"
                  v-model="item.certName"
                />
              </a-form-model-item>
            </a-col>
            <a-col :span="5">
              <a-form-model-item>
                <a-input
                  style="padding-left: 5px"
                  :disabled="disableSubmit"
                  placeholder="请输入"
                  v-model="item.unitName"
                />
              </a-form-model-item>
            </a-col>
            <a-col :span="5">
              <a-form-model-item>
                <a-input
                  style="padding-left: 5px"
                  :disabled="disableSubmit"
                  placeholder="请输入"
                  v-model="item.certNo"
                />
              </a-form-model-item>
            </a-col>
            <a-col :span="5">
              <a-form-model-item>
                <j-date style="width: 100%" :disabled="disableSubmit" v-model="item.certTime" class="value"></j-date>
              </a-form-model-item>
            </a-col>
            <a-col v-if="!disableSubmit" :span="4" style="text-align: center">
              <a-form-model-item>
                <a @click="delRowCustom(index)">删除</a>
              </a-form-model-item>
            </a-col>
          </a-row>
        </div>
      </a-form-model>
    </div>
    <!-- table区域-end -->

    <OrganModal ref="departModal" @ok="updateQueryParam"></OrganModal>
  </j-modal>
</template>
<script>
import { httpAction, getAction } from '@/api/manage'
import OrganModal from '../../components/OrganModal'
import ZUpload from '../../components/ZUpload'
import store from '@/store/'

export default {
  name: 'SysPositionModal',
  components: {
    OrganModal,
    ZUpload,
  },
  data() {
    return {
      departInfo: store.getters.departInfo,
      disableSubmit: false,
      title: '新增专家管理',
      visible: false,
      isCheck: false,
      model: {},
      customRenderList: ['certName', 'unitName', 'certNo', 'certTime'],
      queryParam: {
        unitName: '',
        unit: '',
      },
      url: {
        tree: '/common/knowledgeCommon/queryAllTypesList',
        get: '/sjzyzhgl/sjResourceExpert/queryExpertAuditPersonDetailById',
        edit: '/sjzyzhgl/sjResourceExpert/edit',
        add: '/sjzyzhgl/sjResourceExpert/add',
      },
      treeData: [],
      dataSource: [],
      validatorRules: {
        name: [{ required: true, message: '请选择!' }],
        sex: [{ required: true, message: '请选择!' }],
        politicalStatus: [{ required: true, message: '请选择!' }],
        birthday: [{ required: true, message: '请选择!' }],
        startWorkTime: [{ required: true, message: '请选择!' }],
        unitName: [{ required: true, message: '请选择!' }],
        education: [{ required: true, message: '请选择!' }],
        science: [{ required: true, message: '请选择!' }],
        loginName: [{ required: true, message: '请选择!' }],
        password: [{ required: true, message: '请选择!' }],
      },
    }
  },
  created() {
    this.loadTree()
  },
  methods: {
    add() {
      this.model = {}
      this.dataSource = []
      this.queryParam = {
        unitName: '',
      }
      this.visible = true
    },
    check(record) {
      this.model = Object.assign({}, record)
      this.visible = true
      this.httpRequest()
      this.queryParam = {
        unit: '',
      }
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
      this.httpRequest()
      this.queryParam = {
        unit: '',
      }
    },
    httpRequest() {
      getAction(this.url.get, { id: this.model.id }).then((res) => {
        if (res.success && res.result.length > 0) {
          this.queryParam = res.result[0]
          this.dataSource = JSON.parse(res.result[0].certList)
          this.userType = this.queryParam.userType
        } else {
          console.log(res)
        }
      })
    },
    close(isSubmit) {
      if (isSubmit) {
        this.saveData()
      } else {
        this.visible = false
      }
    },
    saveData() {
      this.$refs.form.validate((valid) => {
        this.queryParam.certList = JSON.stringify(this.dataSource)
        this.queryParam.companyId = this.departInfo.id
        if (valid) {
          let url, type
          if (!this.model.id) {
            url = this.url.add
            type = 'post'
          } else {
            url = this.url.edit
            type = 'put'
          }
          httpAction(url, this.queryParam, type)
            .then((res) => {
              if (res.success) {
                this.$message.success(res.message)
                this.$emit('ok')
                this.visible = false
              } else {
                this.$message.error(res.message)
              }
            })
            .finally(() => {})
        } else {
          return false
        }
      })
    },
    loadTree() {
      this.treeData = []
      getAction(this.url.tree, { type: 2 }).then((res) => {
        if (res.success) {
          this.treeData = res.result
        }
      })
    },
    updateQueryParam(data) {
      this.queryParam.unit = data.id
      this.queryParam.unitName = data.departName
    },
    openOrganModel() {
      this.$refs.departModal.show()
      this.$refs.departModal.selectedKeys = [this.queryParam.unit]
    },
    delRowCustom(index) {
      this.dataSource.splice(index, 1)
    },
    addRowCustom() {
      this.dataSource.push({})
    },
  },
}
</script>

<style lang='less' scoped>
@import '../../css/checkAndEdit.less';

.name-type {
  width: 110px;
  margin-left: 5px;
  border: 1px solid #e0e0e0;

  /deep/ .ant-select-selection {
    box-sizing: border-box;
    min-height: 27px !important;

    .ant-select-selection__rendered {
      line-height: 27px !important;
    }
  }
}

//表格头部样式蓝色
// ::v-deep .ant-table-thead > tr > th {
//   padding-left: px;
//   font-size: px;
//   color:black;
//   background:#F3FAFF !important;
//   border: 1px solid #b8e6ff;
//   border-top: none;
// }

::v-deep .ant-table-thead tr td {
  border: 1px solid #b8e6ff;
}

ant-col ant-form-item-control-wrapper {
  background: white;
}

.isCheck {
  /deep/ .ant-input {
    background-color: #fff;
    padding-left: 10px;
    height: 45px;
    border-radius: 0;
    margin: 0;
  }
}
</style>
